/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EstadisticasForm.java
 *
 * Created on 11-ago-2010, 1:50:49
 */

package stockcontrol;

import entidades.Categoria;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.Stroke;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import javax.persistence.EntityManager;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.border.TitledBorder;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.LogarithmicAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.IntervalMarker;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.Dataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.statistics.HistogramDataset;
import org.jfree.data.statistics.HistogramType;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.Layer;
import org.jfree.ui.RectangleAnchor;
import org.jfree.ui.TextAnchor;

/**
 *
 * @author Cott
 */
public class EstadisticasForm extends javax.swing.JPanel {

    private Vector MESES = new Vector();
    Color[] colors = {
                     new Color(255,0,0,128)     ,
                     new Color(0,255,128,128)   ,
                     new Color(0,0,255,128)   ,
                     new Color(128,0,0,128),
                     new Color(0,128,0,128) ,
                     new Color(0,0,128,128)   ,
                     new Color(255,255,0,128) ,
                     new Color(255,100,0,128) ,
                     new Color(255,0,0,128)   ,
                     new Color(100,0,0,128)
                };
    
    private JFrame ventanaGrafico;
    private JFrame ventanaConvergencia;
    private Estadisticas estadisticas;
    private ParetoForm paretoForm = null;

    EntityManager em;

    private LotePanel lp;



    /** Creates new form EstadisticasForm */
    public EstadisticasForm(EntityManager em) {
        
        this.em = em;

        initComponents();
        //cantRangosSpinner.setValue(3);
        Integer nCat = (Integer) em.createNamedQuery("Categoria.findAllCodigos").getResultList().get(0);
        estadisticas = new Estadisticas(nCat,em);

        List l = (List) em.createNamedQuery("Categoria.findAllNames").getResultList();
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        for (int i = 0; i < l.size(); i++) { 
            String nombre  = (String) l.get(i);
            Categoria categoria = (Categoria) em.createNamedQuery("Categoria.findByNombre").setParameter("nombre", nombre ).getSingleResult();
            List ventas = estadisticas.getCantArtMes(categoria.getCodigo());
            if (!ventas.isEmpty())
                model.addElement(nombre);
        }
        comboCategoria.setModel(model);

        //la categoria seleccionada por el combo
        Categoria cate = (Categoria) em.createNamedQuery("Categoria.findByNombre").setParameter("nombre", (String)(comboCategoria.getSelectedItem()) ).getSingleResult();
        estadisticas.setCategoria(cate);
        
        ventanaGrafico = null;

        String[] meses = {"Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"};
        for (int i = 0; i < meses.length; i++) {
            String string = meses[i];
            MESES.addElement(string);
        }

        lp = new LotePanel();
        lotePanel.setLayout(new BorderLayout());
        lotePanel.add(lp, BorderLayout.CENTER);
    }

    public static class ColorRenderer
    {
        private Color[] color;

        public ColorRenderer(Color[] color)
        {
            this.color = color;
        }

        public void setColor(PiePlot plot, DefaultPieDataset dataset)
        {
            List <Comparable> keys = dataset.getKeys();
            int aInt;

            for (int i = 0; i < keys.size(); i++)
            {
                aInt = i % this.color.length;
                plot.setSectionPaint(keys.get(i), this.color[aInt]);
            }
        }
    }

    void grafico(List data){
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        Categoria cate = (Categoria) em.createNamedQuery("Categoria.findByNombre").setParameter("nombre", (String)(comboCategoria.getSelectedItem()) ).getSingleResult();

        //float[] rangos = estadisticas.getRangos(data, (Integer)cantRangosSpinner.getValue());
        double[] rangos = estadisticas.getRangos();

        // Cargo los datos
        for (int i = 0; i < data.size(); i++) {
                Object[] t = (Object[]) data.get(i);
                int cant = ((Integer) t[2]).intValue();
                int mes = ((Integer) t[0]).intValue();
                // cant, anio, mes
                if ( checkHistograma.isSelected() )
                   dataset.setValue( cant , "", MESES.elementAt(mes - 1).toString() + " " + t[1].toString());
                else
                   dataset.setValue( cant ,t[1].toString(), MESES.elementAt(mes - 1).toString());
        }

        // create the chart...
        JFreeChart chart = ChartFactory.createLineChart(
            "Ventas de " + cate.getNombre().trim() ,                 // chart title
            "Mes",                   // domain axis label
            "Cantidad",              // range axis label
            dataset,                 // data
            PlotOrientation.VERTICAL,// orientation
            (checkHistograma.isSelected() == true)?false:true,                    // include legend
            true,                    // tooltips?
            false                    // URLs?
        );

        // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
        // set the background color for the chart...
        chart.setBackgroundPaint(Color.white);

        // get a reference to the plot for further customisation...
        CategoryPlot plot = chart.getCategoryPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);

        double inf = 0;
        for (int i = 0; i < rangos.length; i++) {
            double sup = rangos[i];
            IntervalMarker target = new IntervalMarker(inf, sup);
            target.setLabel("Rango " + i);
            target.setLabelFont(new Font("SansSerif", Font.ITALIC, 11));
            target.setLabelAnchor(RectangleAnchor.LEFT);
            target.setLabelTextAnchor(TextAnchor.CENTER_LEFT);
            int c = i % colors.length;
            target.setPaint( colors[c] );
            plot.addRangeMarker(target, Layer.BACKGROUND);
            inf = sup;
        }
        // set the range axis to display integers only...
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);

        LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer)plot.getRenderer();
        lineandshaperenderer.setShapesVisible(true);
        lineandshaperenderer.setDrawOutlines(true);
        lineandshaperenderer.setUseFillPaint(true);
        lineandshaperenderer.setFillPaint(Color.white);
        lineandshaperenderer.setStroke(new BasicStroke(3));
        // OPTIONAL CUSTOMISATION COMPLETED.

        // Creación del panel con el gráfico
        ChartPanel panel = new ChartPanel(chart);

        if ( ventanaGrafico == null ){
            ventanaGrafico = new JFrame("Ventas");
            ventanaGrafico.getContentPane().add(panel);      
            ventanaGrafico.setVisible(true);
        } else {
            ventanaGrafico.getContentPane().remove(0);
            ventanaGrafico.getContentPane().add(panel);
        }
        ventanaGrafico.pack();
    }
    
    void graficoTorta(Vector probs){
        DefaultPieDataset dataset = new DefaultPieDataset();
        // Cargo los datos
        int max = 0;
        for (int i = 0; i < probs.size(); i++) {
                Double f = (Double) probs.elementAt(i);
                Double maxVal = (Double) probs.elementAt(max);
                if ( f > maxVal.doubleValue() )
                    max = i;
                dataset.setValue( String.format("Rango %d = %.2f ", i, f*100 ) + "%", f);
        }

        // create the chart...
        JFreeChart chart = ChartFactory.createPieChart(
            "",                 // chart title
            dataset,                  // data
            false,                    // include legend
            true,                     // tooltips?
            false                     // URLs?
        );

        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setExplodePercent(String.format("Rango %d = %.0f ", max, (Double)probs.elementAt(max) * 100 ) + "%", 0.30);
        
         // Specify the colors here
        ColorRenderer renderer = new ColorRenderer(colors);
        renderer.setColor(plot, dataset);       

        //Dar color al fondo del panel
        chart.setBackgroundPaint(Color.WHITE);

        plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
        plot.setNoDataMessage("No data available");
        plot.setCircular(false);

        ChartPanel chartPanel = new ChartPanel(chart);
        piePanel.setLayout(new BorderLayout());
        piePanel.removeAll();
        piePanel.add(chartPanel, BorderLayout.CENTER);
        chartPanel.setSize(piePanel.getSize());
        //piePanel.setBorder(new TitledBorder("Demanda Estimada") );
    }
    
    void graficoConvergencia(Vector data){     
        Vector<Double> probs = (Vector<Double>) data.elementAt(0);
        
        Categoria cate = (Categoria) em.createNamedQuery("Categoria.findByNombre").setParameter("nombre", (String)(comboCategoria.getSelectedItem()) ).getSingleResult();
        // Creo las series
        XYSeries[] series = new XYSeries[probs.size()];
        for (int j = 0; j < probs.size(); j++) {
            XYSeries s = new XYSeries("Rango " + j);
            series[j] = s;
        }

        //las cargo con datos
        for (int i = 0; i < data.size(); i++) {
            probs = (Vector<Double>) data.elementAt(i);
            for (int j = 0; j < series.length; j++) {
                XYSeries xYSeries = series[j];
                xYSeries.add(i+1, probs.elementAt(j) );
            }
        }

        //agrego las series al dataset
        final XYSeriesCollection dataset = new XYSeriesCollection();
        for (int j = 0; j < series.length; j++) {
            XYSeries xYSeries = series[j];
            dataset.addSeries(xYSeries);
        }

        final JFreeChart chart = ChartFactory.createXYLineChart(
            "Grafico de Convergencia",          // chart title
            "Pasos",                            // domain axis label
            "Probabilidad",                     // range axis label
            dataset,                            // data
            PlotOrientation.VERTICAL,
            true,                               // include legend
            true,
            false
        );

        final TextTitle subtitle = new TextTitle("Demanda para " + cate.getNombre().trim());
        chart.addSubtitle(subtitle);
        final XYPlot plot = chart.getXYPlot();
        final NumberAxis domainAxis = new LogarithmicAxis("Pasos");
        final NumberAxis rangeAxis = new NumberAxis("Probabilidad");

        plot.setDomainAxis(domainAxis);
        plot.setRangeAxis(rangeAxis);
        
        chart.setBackgroundPaint(Color.white);
        plot.setOutlinePaint(Color.black);
        plot.setBackgroundPaint(Color.white);
        plot.setDomainGridlinePaint(Color.gray);
        plot.setRangeGridlinePaint(Color.gray);
        
        final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        for (int i = 0; i < colors.length; i++) {
            int c = i % colors.length;
            renderer.setSeriesPaint(i, colors[c]);
        }   
        renderer.setBaseShapesVisible(false);
        plot.setRenderer(renderer);

        // Creación del panel con el gráfico
        ChartPanel panel = new ChartPanel(chart);

        if ( ventanaConvergencia == null ){
            ventanaConvergencia = new JFrame("Grafico de convergencia");
            ventanaConvergencia.getContentPane().add(panel);
            ventanaConvergencia.setVisible(true);
        } else {
            ventanaConvergencia.getContentPane().remove(0);
            ventanaConvergencia.getContentPane().add(panel);
            ventanaConvergencia.setVisible(true);
        }
        ventanaConvergencia.pack();       
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        rangosGroup = new javax.swing.ButtonGroup();
        fechasGroup = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        comboCategoria = new javax.swing.JComboBox();
        buttonCalcular = new javax.swing.JButton();
        jXTitledSeparator1 = new org.jdesktop.swingx.JXTitledSeparator();
        jXTitledSeparator2 = new org.jdesktop.swingx.JXTitledSeparator();
        lotePanel = new javax.swing.JPanel();
        buttonPareto = new javax.swing.JButton();
        checkConvergencia = new javax.swing.JCheckBox();
        markovPanel = new javax.swing.JPanel();
        piePanel = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        cantRangosSpinner = new javax.swing.JSpinner();
        jLabel3 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        fechasTodas = new javax.swing.JRadioButton();
        selectFechas = new javax.swing.JRadioButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        aniosOutList = new javax.swing.JList();
        add = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        aniosInList = new javax.swing.JList();
        rem = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        mesesOutList = new javax.swing.JList();
        addMeses = new javax.swing.JButton();
        remMeses = new javax.swing.JButton();
        jScrollPane6 = new javax.swing.JScrollPane();
        mesesInList = new javax.swing.JList();
        buttonGrafico = new javax.swing.JButton();
        checkHistograma = new javax.swing.JCheckBox();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(stockcontrol.StockControlApp.class).getContext().getResourceMap(EstadisticasForm.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        comboCategoria.setName("comboCategoria"); // NOI18N

        buttonCalcular.setText(resourceMap.getString("buttonCalcular.text")); // NOI18N
        buttonCalcular.setName("buttonCalcular"); // NOI18N
        buttonCalcular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCalcularActionPerformed(evt);
            }
        });

        jXTitledSeparator1.setTitle(resourceMap.getString("jXTitledSeparator1.title")); // NOI18N
        jXTitledSeparator1.setName("jXTitledSeparator1"); // NOI18N

        jXTitledSeparator2.setTitle(resourceMap.getString("jXTitledSeparator2.title")); // NOI18N
        jXTitledSeparator2.setName("jXTitledSeparator2"); // NOI18N

        lotePanel.setName("lotePanel"); // NOI18N
        lotePanel.setPreferredSize(new java.awt.Dimension(556, 343));

        javax.swing.GroupLayout lotePanelLayout = new javax.swing.GroupLayout(lotePanel);
        lotePanel.setLayout(lotePanelLayout);
        lotePanelLayout.setHorizontalGroup(
            lotePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 800, Short.MAX_VALUE)
        );
        lotePanelLayout.setVerticalGroup(
            lotePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 183, Short.MAX_VALUE)
        );

        buttonPareto.setText(resourceMap.getString("buttonPareto.text")); // NOI18N
        buttonPareto.setName("buttonPareto"); // NOI18N
        buttonPareto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonParetoActionPerformed(evt);
            }
        });

        checkConvergencia.setText(resourceMap.getString("checkConvergencia.text")); // NOI18N
        checkConvergencia.setName("checkConvergencia"); // NOI18N
        checkConvergencia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkConvergenciaActionPerformed(evt);
            }
        });

        markovPanel.setName("markovPanel"); // NOI18N

        piePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("piePanel.border.title"))); // NOI18N
        piePanel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        piePanel.setMaximumSize(new java.awt.Dimension(304, 238));
        piePanel.setName("piePanel"); // NOI18N
        piePanel.setLayout(new java.awt.GridLayout(1, 1));

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        cantRangosSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(3), Integer.valueOf(2), null, Integer.valueOf(1)));
        cantRangosSpinner.setName("cantRangosSpinner"); // NOI18N
        cantRangosSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                cantRangosSpinnerStateChanged(evt);
            }
        });

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel2.border.title"))); // NOI18N

        fechasGroup.add(fechasTodas);
        fechasTodas.setSelected(true);
        fechasTodas.setText(resourceMap.getString("fechasTodas.text")); // NOI18N
        fechasTodas.setName("fechasTodas"); // NOI18N

        fechasGroup.add(selectFechas);
        selectFechas.setText(resourceMap.getString("selectFechas.text")); // NOI18N
        selectFechas.setName("selectFechas"); // NOI18N
        selectFechas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectFechasActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel3.border.title"))); // NOI18N
        jPanel3.setName("jPanel3"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, selectFechas, org.jdesktop.beansbinding.ELProperty.create("${selected}"), jPanel3, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        aniosOutList.setName("aniosOutList"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, selectFechas, org.jdesktop.beansbinding.ELProperty.create("${selected}"), aniosOutList, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jScrollPane3.setViewportView(aniosOutList);

        add.setText(resourceMap.getString("add.text")); // NOI18N
        add.setName("add"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, selectFechas, org.jdesktop.beansbinding.ELProperty.create("${selected}"), add, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addActionPerformed(evt);
            }
        });

        jScrollPane4.setName("jScrollPane4"); // NOI18N

        aniosInList.setName("aniosInList"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, selectFechas, org.jdesktop.beansbinding.ELProperty.create("${selected}"), aniosInList, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jScrollPane4.setViewportView(aniosInList);

        rem.setText(resourceMap.getString("rem.text")); // NOI18N
        rem.setName("rem"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, selectFechas, org.jdesktop.beansbinding.ELProperty.create("${selected}"), rem, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        rem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                remActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rem, 0, 0, Short.MAX_VALUE)
                    .addComponent(add, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jScrollPane3, jScrollPane4});

        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel3Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(add)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(rem))
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)))
                .addContainerGap())
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel4.border.title"))); // NOI18N
        jPanel4.setName("jPanel4"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, selectFechas, org.jdesktop.beansbinding.ELProperty.create("${selected}"), jPanel4, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jScrollPane5.setName("jScrollPane5"); // NOI18N

        mesesOutList.setName("mesesOutList"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, selectFechas, org.jdesktop.beansbinding.ELProperty.create("${selected}"), mesesOutList, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jScrollPane5.setViewportView(mesesOutList);

        addMeses.setText(resourceMap.getString("addMeses.text")); // NOI18N
        addMeses.setName("addMeses"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, selectFechas, org.jdesktop.beansbinding.ELProperty.create("${selected}"), addMeses, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        addMeses.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addMesesActionPerformed(evt);
            }
        });

        remMeses.setText(resourceMap.getString("remMeses.text")); // NOI18N
        remMeses.setName("remMeses"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, selectFechas, org.jdesktop.beansbinding.ELProperty.create("${selected}"), remMeses, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        remMeses.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                remMesesActionPerformed(evt);
            }
        });

        jScrollPane6.setName("jScrollPane6"); // NOI18N

        mesesInList.setName("mesesInList"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, selectFechas, org.jdesktop.beansbinding.ELProperty.create("${selected}"), mesesInList, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jScrollPane6.setViewportView(mesesInList);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(remMeses, 0, 0, Short.MAX_VALUE)
                    .addComponent(addMeses))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel4Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jScrollPane5, jScrollPane6});

        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(addMeses)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(remMeses))
                    .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(fechasTodas)
                    .addComponent(selectFechas))
                .addContainerGap(330, Short.MAX_VALUE))
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(3, 3, 3)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(14, Short.MAX_VALUE)))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(fechasTodas)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(selectFechas)
                .addContainerGap(128, Short.MAX_VALUE))
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(58, 58, 58)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addContainerGap()))
        );

        buttonGrafico.setText(resourceMap.getString("buttonGrafico.text")); // NOI18N
        buttonGrafico.setName("buttonGrafico"); // NOI18N
        buttonGrafico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonGraficoActionPerformed(evt);
            }
        });

        checkHistograma.setText(resourceMap.getString("checkHistograma.text")); // NOI18N
        checkHistograma.setName("checkHistograma"); // NOI18N

        javax.swing.GroupLayout markovPanelLayout = new javax.swing.GroupLayout(markovPanel);
        markovPanel.setLayout(markovPanelLayout);
        markovPanelLayout.setHorizontalGroup(
            markovPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(markovPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(markovPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(markovPanelLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cantRangosSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonGrafico, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(checkHistograma))
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(piePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(19, Short.MAX_VALUE))
        );
        markovPanelLayout.setVerticalGroup(
            markovPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(markovPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(markovPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(piePanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, markovPanelLayout.createSequentialGroup()
                        .addGroup(markovPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cantRangosSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(buttonGrafico)
                            .addComponent(checkHistograma))
                        .addGap(11, 11, 11)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(jXTitledSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 801, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(comboCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, 315, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(buttonCalcular, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(checkConvergencia)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonPareto)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(markovPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(19, 19, 19))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(1, 1, 1)
                                .addComponent(lotePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE))
                            .addComponent(jXTitledSeparator2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 801, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(22, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonCalcular)
                    .addComponent(checkConvergencia)
                    .addComponent(buttonPareto)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addComponent(jXTitledSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(markovPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(3, 3, 3)
                .addComponent(jXTitledSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lotePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
                .addContainerGap())
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonCalcularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCalcularActionPerformed
        // TODO add your handling code here:      
        Categoria categoria = (Categoria) em.createNamedQuery("Categoria.findByNombre").setParameter("nombre", (String)(comboCategoria.getSelectedItem()) ).getSingleResult();
        estadisticas.setCategoria(categoria);
        
        List newData = new Vector();
        if (selectFechas.isSelected()) {
            estadisticas.setAnios(this.getAnios());
            estadisticas.setMeses(this.getMeses());
        }
        newData = estadisticas.getDataVentas();
        if (newData.isEmpty()){
            JOptionPane.showMessageDialog(this,
                "La categoria seleccionada no posee ventas para las fechas seleccionadas.",
                "Categoria sin ventas",
                JOptionPane.WARNING_MESSAGE);
            return;
        }

        estadisticas.setCantRangos((Integer) cantRangosSpinner.getValue() );
        Vector v_estrella = estadisticas.calcularDistribucionDemanda();

        this.grafico(newData);
        graficoTorta(v_estrella);
        System.out.println("v*");
        for (int i = 0; i < v_estrella.size(); i++) 
            System.out.println(v_estrella.elementAt(i));



        lp.calcularLote(estadisticas);
        
         //dibujar la convergencia
        //estadisticas.salvarDatosConvergencia();
        if ( checkConvergencia.isSelected() )
            this.graficoConvergencia(estadisticas.getDatosConvergencia());

    }//GEN-LAST:event_buttonCalcularActionPerformed

    private void cantRangosSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_cantRangosSpinnerStateChanged
        // TODO add your handling code here:

    }//GEN-LAST:event_cantRangosSpinnerStateChanged

    private void buttonGraficoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonGraficoActionPerformed
        // TODO add your handling code here:
        Categoria categoria = (Categoria) em.createNamedQuery("Categoria.findByNombre").setParameter("nombre", (String)(comboCategoria.getSelectedItem()) ).getSingleResult();
        estadisticas.setCategoria(categoria);

        List newData = new Vector();
        if (selectFechas.isSelected()) {
            estadisticas.setMeses(this.getMeses());
            estadisticas.setAnios(this.getAnios());
        }    
        newData = estadisticas.getDataVentas();
        if (newData.isEmpty()){
            JOptionPane.showMessageDialog(this,
                "La categoria seleccionada no posee ventas para las fechas seleccionadas.",
                "Categoria sin ventas",
                JOptionPane.WARNING_MESSAGE);
            return;
        }
        estadisticas.setCantRangos((Integer) cantRangosSpinner.getValue() );
        //if ( checkHistograma.isSelected() )
        //    this.graficoHistograma(newData);
        //else
            this.grafico(newData);
    }//GEN-LAST:event_buttonGraficoActionPerformed

    private void selectFechasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectFechasActionPerformed
        // TODO add your handling code here:   
        Categoria categoria = (Categoria) em.createNamedQuery("Categoria.findByNombre").setParameter("nombre", (String)(comboCategoria.getSelectedItem()) ).getSingleResult();
        List l = estadisticas.getCantArtMes(categoria.getCodigo());

        Vector meses = new Vector();
        Vector anios = new Vector();

        // Cargo los datos
        for (int i = 0; i < l.size(); i++) {
            Object[] t = (Object[]) l.get(i);
            int mes = ((Integer) t[0]).intValue();
            int anio = ((Integer) t[1]).intValue();
            if ( !meses.contains(mes) )
                meses.add(mes);
            if ( !anios.contains(anio) )
                anios.add(anio);
        }

        this.cargarFechas(anios, meses);
    }//GEN-LAST:event_selectFechasActionPerformed

    private void addActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addActionPerformed
        // TODO add your handling code here:
        Object[] anios = (Object[]) aniosOutList.getSelectedValues();

        for (int i = 0; i < anios.length; i++) {
            ((DefaultListModel)(aniosOutList.getModel())).removeElement(anios[i]);
            ((DefaultListModel)(aniosInList.getModel())).addElement(anios[i]);
        }
        this.update();
    }//GEN-LAST:event_addActionPerformed

    private void remActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remActionPerformed
        // TODO add your handling code here:
        Object[] anios = (Object[]) aniosInList.getSelectedValues();

        for (int i = 0; i < anios.length; i++) {
            Object o = anios[i];
            ((DefaultListModel) (aniosOutList.getModel())).addElement(o);
            ((DefaultListModel) (aniosInList.getModel())).removeElement(o);
        }
        this.update();
    }//GEN-LAST:event_remActionPerformed

    private void addMesesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addMesesActionPerformed
        // TODO add your handling code here:
        Object[] meses = (Object[]) mesesOutList.getSelectedValues();

        for (int i = 0; i < meses.length; i++) {
            ((DefaultListModel)(mesesOutList.getModel())).removeElement(meses[i]);
            ((DefaultListModel)(mesesInList.getModel())).addElement(meses[i]);
        }
        this.update();
}//GEN-LAST:event_addMesesActionPerformed

    private void remMesesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remMesesActionPerformed
        // TODO add your handling code here:
        Object[] meses = (Object[]) mesesInList.getSelectedValues();

        for (int i = 0; i < meses.length; i++) {
            Object o = meses[i];
            ((DefaultListModel) (mesesOutList.getModel())).addElement(o);
            ((DefaultListModel) (mesesInList.getModel())).removeElement(o);
        }
        this.update();

}//GEN-LAST:event_remMesesActionPerformed

    private void buttonParetoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonParetoActionPerformed
        // TODO add your handling code here:
        double demanda = (estadisticas.getDemandaEstimada()[0] + estadisticas.getDemandaEstimada()[1])/2.0;
        paretoForm = new ParetoForm( estadisticas.getCategoria(), demanda, em);
        paretoForm.setDefaultCloseOperation(paretoForm.DISPOSE_ON_CLOSE);
        paretoForm.setVisible(true);
        paretoForm.pack();

    }//GEN-LAST:event_buttonParetoActionPerformed

    private void checkConvergenciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkConvergenciaActionPerformed
        // TODO add your handling code here:
        Vector v = estadisticas.getDatosConvergencia();
        if ( !v.isEmpty() )
            this.graficoConvergencia(v);
    }//GEN-LAST:event_checkConvergenciaActionPerformed

    void update(){
       estadisticas.setMeses(this.getMeses());
       estadisticas.setAnios(this.getAnios());

       List newData = estadisticas.getDataVentas();
       
       this.grafico(newData);
    }

    // devuelve los meses seleccionados por el usuario (en forma de nros, para agosto, devuelve 8)
    List getMeses(){
        Enumeration elsMeses = ((DefaultListModel) (mesesInList.getModel())).elements();
        List lm = new Vector(Collections.list(elsMeses));
        List meses = new Vector();

        for (int i = 0; i < lm.size(); i++) {
            String s = (String)lm.get(i);
            meses.add( MESES.indexOf(s) + 1 );
        }
        return meses;
    }
    
    List getAnios(){
        Enumeration elsAnios = ((DefaultListModel) (aniosInList.getModel())).elements();
        List la = new Vector(Collections.list(elsAnios));
        return la;
    }

    void cargarFechas(List anios, List meses){
        DefaultListModel aniosModel = new DefaultListModel();
        DefaultListModel mesesModel = new DefaultListModel();

        mesesInList.setModel(mesesModel);
        mesesOutList.setModel(new DefaultListModel());
        
        for (int i = 0; i < anios.size(); i++) {
            Integer a = (Integer) anios.get(i);
            aniosModel.add(i, a);
        }

        for (int i = 0; i < meses.size(); i++) {
            int m  = ((Integer)meses.get(i));
            String a = (String) MESES.elementAt( m-1 );
            mesesModel.add(i, a);
        }

        aniosInList.setModel(aniosModel);
        aniosOutList.setModel(new DefaultListModel());
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton add;
    private javax.swing.JButton addMeses;
    private javax.swing.JList aniosInList;
    private javax.swing.JList aniosOutList;
    private javax.swing.JButton buttonCalcular;
    private javax.swing.JButton buttonGrafico;
    private javax.swing.JButton buttonPareto;
    private javax.swing.JSpinner cantRangosSpinner;
    private javax.swing.JCheckBox checkConvergencia;
    private javax.swing.JCheckBox checkHistograma;
    private javax.swing.JComboBox comboCategoria;
    private javax.swing.ButtonGroup fechasGroup;
    private javax.swing.JRadioButton fechasTodas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private org.jdesktop.swingx.JXTitledSeparator jXTitledSeparator1;
    private org.jdesktop.swingx.JXTitledSeparator jXTitledSeparator2;
    private javax.swing.JPanel lotePanel;
    private javax.swing.JPanel markovPanel;
    private javax.swing.JList mesesInList;
    private javax.swing.JList mesesOutList;
    private javax.swing.JPanel piePanel;
    private javax.swing.ButtonGroup rangosGroup;
    private javax.swing.JButton rem;
    private javax.swing.JButton remMeses;
    private javax.swing.JRadioButton selectFechas;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

     public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("Estadisticas");
                frame.setContentPane(new EstadisticasForm(null));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}
